/******************************************************************************************
* Project: Workhorses of Opportunity — Howard and Weinstein
*
* Inputs:
*   - "tfs_cleaned.dta"
*
* Outputs:
*   - Coefficient plots:
*       - "coefplot_compliers.pdf"
*       - "coefplot_frictions.pdf"
*       - "coefplot_compliers_fe.pdf"
*       - "coefplot_frictions_fe.pdf"
******************************************************************************************/

use "tfs_cleaned", clear

* Prepare demographic and SAT variables
gen age20orover = ageUSE>=20
replace age20orover = . if ageUSE==.

gen sattothun = sattot/1000
replace ageUSE = ageUSE/10

* List of outcome variables to be analyzed
local vars  c_visits  c_parentss  c_teachers  c_prvtcounss c_notafffirsts c_costs  c_lowtuits  f_jobs   f_ftjobs  a_fam_c a_loan_c r_awayhomes 	r_jobs r_nojobs c_goodjobss  c_acadreputs   c_topgrads r_cultures  c_earlys  r_findpurps  numapplyone sattothun rate01 f_transfs  firstchoice fatheduc2 fatheduc3 fatheduc5 fatheduc6 fatheduc8 motheduc2 motheduc3 motheduc5 motheduc6 motheduc8 white lninc_midpoint   planlivefam c_nearhomes r_friendsgoings c_friendsatts  ageUSE c_onlines c_friendsuggs c_liveathomes

keep `vars' home_hasnormalschool homestate homectyfips year studwgt

* Loop over outcomes: run two reghdfe models, with and without year FE
timer clear
foreach x in `vars' {
	disp "`x'"
	timer on 1
	qui {
		preserve
			keep `x' home_hasnormalschool homestate homectyfips studwgt
			collapse `x' [w=studwgt], by(home_hasnormalschool homectyfips homestate)

			reghdfe `x' home_hasnormalschool, absorb(homestate) cluster(homestate)
			estimates store `x'
		restore

		bys homectyfips: egen numobs = total(studwgt * (`x' != .))
		gen myweight = 1/numobs * studwgt
		reghdfe `x' home_hasnormalschool [aw=myweight], absorb(homestate#year) cluster(homestate)
		estimates store `x'_yearfe

		drop numobs myweight
	}
	timer off 1
	timer list 1
}

* Label variables for interpretability in plots
cap label var ageUSE "Age (10s)"
cap label var c_visitv "Chose college because of visit; V. Important"
cap label var c_visits "Chose college because of visit"
cap label var c_onlines "Chose college because of online ed."
cap label var c_friendsuggs "Chose college because of friend"
cap label var c_hscounss "Chose college because of HS counselor"
cap label var c_liveathomes "Chose college because wanted to live at home"
cap label var c_parentsv "Chose college because of parents; V. Important"
cap label var c_parentss "Chose college because of parents"
cap label var c_prvtcounsv "Chose college because of private counselor; V. Important"
cap label var c_prvtcounss "Chose college because of private counselor"
cap label var planlivefam "Plan on living with family freshman year"
cap label var c_teacherv "Chose college because of teacher; V. Important"
cap label var c_teachers "Chose college because of teacher"
cap label var c_notafffirstv "Chose college because could not afford 1st choice; V. Important"
cap label var c_notafffirsts "Chose college because could not afford 1st choice"
cap label var c_costv "Chose college because cost; V. Important"
cap label var c_costs "Chose college because cost"
cap label var c_lowtuitv "Chose college because low tuition; V. Important"
cap label var c_lowtuits "Chose college because low tuition"
cap label var f_jobv "Likely to have job to pay for college; V. Good"
cap label var f_jobs "Likely to have job to pay for college"
cap label var f_ftjobv "Likely to have full-time job in college; V. Good"
cap label var f_ftjobs "Likely to have full-time job in college"
cap label var a_fam_c "Financing college via family and friends"
cap label var a_loan_c "Financing college via loans"
cap label var r_awayhomev "Going to college to get away from home; V. Important"
cap label var r_awayhomes "Going to college to get away from home"
cap label var r_jobv "Going to college for better job; V. Important"
cap label var r_jobs "Going to college for better job"
cap label var r_nojobv "Going to college because no job; V. Important"
cap label var r_nojobs "Going to college because no job"
cap label var c_goodjobsv "Chose college because of graduates' jobs; V. Important"
cap label var c_goodjobss "Chose college because of graduates' jobs"
cap label var c_acadreputv "Chose college because of academic reputation; V. Important"
cap label var c_acadreputs "Chose college because academic reputation"
cap label var c_topgradv "Chose college because grads' in top grad. sch.; V. Important"
cap label var c_topgrads "Chose college because grads' in top grad. sch."
cap label var r_culturev "Going to college to become cultured; V. Important"
cap label var r_cultures "Going to college to become cultured"
cap label var c_earlyv "Chose college because applied early; V. Important"
cap label var c_earlys "Chose college because applied early"
cap label var r_findpurpv "Going to college to find purpose; V. Important"
cap label var r_findpurps "Going to college to find purpose"
cap label var c_nearhomes "Chose college because wanted to live near home"
cap label var r_friendsgoings "Going to college because friends going"
cap label var c_friendsatts "Chose college because friends attending"
cap label var numapplyone "Applied to one college"
cap label var sattot "SAT score"
cap label var sattothun "SAT score (1000s)"
cap label var rate01 "Self-rating: academic ability"
cap label var f_transfv "Likely to Transfer; V. Good"
cap label var f_transfs "Likely to Transfer"
cap label var firstchoice "This college was first choice"
cap label var fatheduc2 "Dad educ: Some HS"
cap label var fatheduc3 "Dad educ: HS"
cap label var fatheduc5 "Dad educ: Some college"
cap label var fatheduc6 "Dad educ: College"
cap label var fatheduc8 "Dad educ: Grad. sch"
cap label var motheduc2 "Mom educ: Some HS"
cap label var motheduc3 "Mom educ: HS"
cap label var motheduc5 "Mom educ: Some college"
cap label var motheduc6 "Mom educ: College"
cap label var motheduc8 "Mom educ: Grad. sch"
cap label var white "Race: White"
cap label var lninc_midpoint "Log parent income"

* Define conceptually grouped variable lists for plotting
local compliers c_earlys numapplyone sattothun ageUSE white lninc_midpoint motheduc3 motheduc8 r_nojobs r_jobs c_goodjobss c_acadreputs r_cultures r_findpurps  
local frictions c_notafffirsts f_jobs f_ftjobs c_liveathomes planlivefam a_fam_c a_loan_c f_transfs c_onlines c_visits c_parentss c_teachers c_prvtcounss c_friendsuggs c_nearhomes r_friendsgoings c_friendsatts 

* Coefficient plots (main regressions)
coefplot `compliers', asequation swapnames drop(_cons) xline(0, lpattern(dash) lcolor(gs8)) legend(off) grid(none) ci(95 90) ciopts(recast(rspike rcap) lcolor(dkgreen dkgreen)) mcolor(dkgreen)
graph rename compliers, replace
graph export "coefplot_compliers.pdf", as(pdf) replace

coefplot `frictions', asequation swapnames drop(_cons) xline(0, lpattern(dash) lcolor(gs8)) legend(off) grid(none) ci(95 90) ciopts(recast(rspike rcap) lcolor(dkgreen dkgreen)) mcolor(dkgreen)
graph rename frictions, replace
graph export "coefplot_frictions.pdf", as(pdf) replace

* Restore and rename regressions to generate year-FE versions
foreach x in `vars' {
	disp "`x'"
	est restore `x'
	est store `x'_noyearfe

	est restore `x'_yearfe
	est store `x'
}

* Coefficient plots (year-FE regressions)
coefplot `compliers', asequation swapnames drop(_cons) xline(0, lpattern(dash) lcolor(gs8)) legend(off) grid(none)  ci(95 90) ciopts(recast(rspike rcap) lcolor(dkgreen dkgreen)) mcolor(dkgreen)
graph rename compliers_yearfe, replace
graph export "coefplot_compliers_fe.pdf", as(pdf) replace

coefplot `frictions', asequation swapnames drop(_cons) xline(0, lpattern(dash) lcolor(gs8)) legend(off) grid(none) ci(95 90) ciopts(recast(rspike rcap) lcolor(dkgreen dkgreen)) mcolor(dkgreen)
graph rename frictions_yearfe, replace
graph export "coefplot_frictions_fe.pdf", as(pdf) replace
